package com.dongao.marketing.common.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import org.apache.commons.io.IOUtils;
import org.springframework.web.util.HtmlUtils;


public class ExportBigData {
	
	StringBuffer headStr = new StringBuffer(  
            "<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\">")  
            .append("<head>")  
            .append(  
                    "<meta http-equiv=\"content-type\" content=\"application/ms-excel; charset=UTF-8\"/>")  
            .append("<!--[if gte mso 9]><xml>").append("<x:ExcelWorkbook>")  
            .append("<x:ExcelWorksheets>").append("<x:ExcelWorksheet>").append(  
                    "<x:Name></x:Name>").append("<x:WorksheetOptions>").append(  
                    "<x:Print>").append("<x:ValidPrinterInfo />").append(  
                    "</x:Print>").append("</x:WorksheetOptions>").append(  
                    "</x:ExcelWorksheet>").append("</x:ExcelWorksheets>")  
            .append("</x:ExcelWorkbook>").append("</xml><![endif]-->").append(  
                    "</head>").append("<body>").append("<table>");  
  
    StringBuffer footStr = new StringBuffer("</table></body></html>"); 
	
	/** 
     * 数据输出 
     *  
     * @param data 
     * @param fos 
     * @throws IOException 
     */  
    public void writeToOutputStream(String data, FileOutputStream fos)  
            throws IOException {  
        IOUtils.write(data, fos, "UTF-8");  
    }  
    
    /** 
     * 临时文件前缀 
     *  
     * @return 
     */  
    protected String getPrefix() {  
        return "excelTemp";  
    }  
  
    /** 
     * 临时文件后缀 
     *  
     * @return 
     */  
    protected String getSuffix() {  
        return ".xls";  
    } 
    
    /** 
     * 文件开头的写入 
     *  
     * @param fos 
     * @throws IOException 
     */  
    public void writeHeaderToOutputStream(FileOutputStream fos)  
            throws IOException {  
        writeToOutputStream(headStr.toString(), fos);  
    }  
  
    /** 
     * 文件结尾的写入 
     *  
     * @param fos 
     */  
    public void writeFooterToOutputStream(FileOutputStream fos)  
            throws IOException {  
        writeToOutputStream(footStr.toString(), fos);  
    }  
    
    /** 
     * 创建临时文件 
     *  
     * @return 
     * @throws IOException 
     */  
    public File createTempFile() throws IOException {  
        return File.createTempFile(getPrefix(), getSuffix());  
    }  
    
    /** 
     * 写表头
     *  
     * @return 
     * @throws IOException 
     */
    public void writeTitleToOutputStream(String[] headers,  
            FileOutputStream fos) throws IOException {  
    	writeToOutputStream("<tr>", fos); 
    	int length=headers.length;
    	for (int i = 0; i < length; i++) {
    		writeToOutputStream("<td>"  
                    + HtmlUtils.htmlEscape(headers[i])  
                    + "</td>", fos);  
		}
    	writeToOutputStream("</tr>", fos); 
    }  
    
    /** 
     * 打包 压缩成zip 
     *  
     * @param os 
     *            压缩输出流 
     * @param fileList 
     *            被压缩的文件列表 
     * @throws IOException 
     */  
    public void doZip(OutputStream os, List<File> fileList)  
            throws IOException {  
        if (fileList != null && fileList.size() > 0) {  
            byte[] buf = new byte[1024];  
            ZipOutputStream out = new ZipOutputStream(os);  
            for (File file : fileList) {  
                FileInputStream in = new FileInputStream(file);  
                out.putNextEntry(new ZipEntry(file.getName()));  
                int len;  
                while ((len = in.read(buf)) > 0) {  
                    out.write(buf, 0, len);  
                }  
                out.closeEntry();  
                in.close();  
            }  
            out.close();  
        }  
    }  
    
    /** 
     * 删除临时文件 
     *  
     * @param fileList 
     */  
    public void cleanTempFile(List<File> fileList) {  
        for (File file : fileList) {  
            System.out.println("-----删除文件," + file.getPath());  
            file.delete();  
        }  
    }
    
    

}
